package com.kzd.gtboon

import android.annotation.SuppressLint
import android.content.Intent
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.*
import com.kzd.gtboon.app.GTApplication
import com.kzd.gtboon.app.GT_FORCE_LOGIN
import com.kzd.gtboon.app.SPSConstance
import com.kzd.gtboon.base.BaseViewModel
import com.kzd.gtboon.main.*
import com.module.lemlin.owner.OwnerAbstractActivity
import com.module.lemlin.owner.ownerViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@SuppressLint("CustomSplashScreen")
class SplashActivity : OwnerAbstractActivity() {

    private var mTimeMillis: Long = 500

    private val mViewModel: BaseViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override fun initView() {
        toActivity()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AGREE_REQUEST_CODE && SPSConstance.isReadAgree == true) {
            mTimeMillis = 0
            if ((applicationContext as GTApplication).initSDK()) {
                toActivity()
            }
        }
    }

    private fun toActivity() {
        mViewModel.viewModelScope.launch(Dispatchers.Main) {
            delay(mTimeMillis)
            if (SPSConstance.isReadAgree == false) {
                val intent = Intent(this@SplashActivity,AgreeActivity::class.java)
                startActivityForResult(intent,AGREE_REQUEST_CODE)
                return@launch
            }
            if (!GT_FORCE_LOGIN || checkLogin()) {
                toMainActivity()
            } else {
                toAccessActivity(AccessAction.LOGIN_CHANNEL)
            }
            finish()
        }
    }
}